package com.aldjob.controllerImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.project.XXXX.inter.*;
import com.project.XXXX.model.*;
import com.project.XXXX.stringmodel.PtJobForShown;
import com.project.XXXX.stringmodel.PtJobForShownWithBlobs;
import com.project.XXXX.tools.ProjectTools;

public class PtController {
	private tbl_ptjobMapper tbl_ptjobMapper;
	private tbl_dictMapper tbl_dictMapper;
	private tbl_workplaceMapper tbl_workplaceMapper;
	private tbl_divisionMapper tbl_divisionMapper;
	private tbl_employerinfoMapper tbl_employerinfoMapper;
	private tbl_jobenrollMapper tbl_jobenrollMapper;

	public PtController(com.project.XXXX.inter.tbl_ptjobMapper tbl_ptjobMapper,
			com.project.XXXX.inter.tbl_dictMapper tbl_dictMapper,
			com.project.XXXX.inter.tbl_workplaceMapper tbl_workplaceMapper,
			com.project.XXXX.inter.tbl_divisionMapper tbl_divisionMapper,
			com.project.XXXX.inter.tbl_employerinfoMapper tbl_employerinfoMapper,
			com.project.XXXX.inter.tbl_jobenrollMapper tbl_jobenrollMapper) {
		super();
		this.tbl_ptjobMapper = tbl_ptjobMapper;
		this.tbl_dictMapper = tbl_dictMapper;
		this.tbl_workplaceMapper = tbl_workplaceMapper;
		this.tbl_divisionMapper = tbl_divisionMapper;
		this.tbl_employerinfoMapper = tbl_employerinfoMapper;
		this.tbl_jobenrollMapper = tbl_jobenrollMapper;
	}

	public int getCountByTypeandPlace(String type, String country) {
		Map<String, String> map = new HashMap<String, String>();

		if (!StringUtils.isBlank(type)) {
			map.put("_pttypeid", type);
		}
		if (!StringUtils.isBlank(country)) {
			if (StringUtils.endsWith(country, "00"))
				country = country.substring(0, 4) + "%";
			else
				country = country + "%";
			map.put("_cityid", country);
		}

		return tbl_ptjobMapper.showByTypeandPlaceCount(map);
	}

	public List<PtJobForShown> getByTypeandPlace(int currentPage, String type, String country) {
		Map<String, Object> map = new HashMap<String, Object>();
		int pageSize = 5;
		currentPage = (currentPage - 1) * pageSize;
		map.put("pagesize", pageSize);
		map.put("offset", currentPage);

		if (!StringUtils.isBlank(type)) {
			map.put("_pttypeid", type);
		}
		if (!StringUtils.isBlank(country)) {
			if (StringUtils.endsWith(country, "00"))
				country = country.substring(0, 4) + "%";
			else
				country = country + "%";
			map.put("_cityid", country);
		}
		List<tbl_ptjobWithBLOBs> list = tbl_ptjobMapper.showByTypeandPlace(map);

		// System.out.println(list.size());
		List<PtJobForShown> l = new ArrayList<PtJobForShown>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		String pttypeid;
		String cityid;
		String salaryunit;
		String billcycleid;
		String genderneedid;
		String interview;
		String train;
		tbl_employerinfo tbl_employerinfo;
		int applyed,confirmed;
		for (tbl_ptjobWithBLOBs job : list) {
			map.clear();
			tbl_employerinfo = tbl_employerinfoMapper.selectByPrimaryKey(job.getMobile());
			cityid = tbl_divisionMapper.selectCountryName(job.getCityid() + "").getDiming();
			salaryunit = tbl_dictMapper.selectByPrimaryKey(job.getSalaryunit()).getValue();
			pttypeid = tbl_dictMapper.selectByPrimaryKey(job.getPttypeid()).getValue();
			billcycleid = tbl_dictMapper.selectByPrimaryKey(job.getBillcycleid()).getValue();
			genderneedid = tbl_dictMapper.selectByPrimaryKey(job.getGenderneedid()).getValue();
			interview = tbl_dictMapper.selectByPrimaryKey(job.getInterview()).getValue();
			train = tbl_dictMapper.selectByPrimaryKey(job.getTrain()).getValue();
			
			map.put("ptjobid", job.getId());
			map.put("status", 30);
			applyed = tbl_jobenrollMapper.getReqJobUserCount(map);
			map.clear();
			map.put("ptjobid", job.getId());
			map.put("status", 31);
			confirmed = tbl_jobenrollMapper.getReqJobUserCount(map);
			PtJobForShown ptjob = new PtJobForShown(job.getId() + "", job.getMobile(), job.getTitle(), pttypeid, cityid,
					sdf.format(job.getBegindate()), sdf.format(job.getEnddate()), job.getPersonnum() + "", genderneedid,
					job.getSalary() + "", salaryunit, billcycleid, sdf.format(job.getCreatetime()), interview,train ,
					job.getTimebegin(), job.getTimeend(), tbl_employerinfo.getLogo(), applyed,confirmed,tbl_employerinfo.getName());
			l.add(ptjob);
		}
		return l;
	}

	public PtJobForShownWithBlobs getDetail(int ptid) {
		tbl_ptjobWithBLOBs job = tbl_ptjobMapper.selectByPrimaryKey(ptid);
		PtJobForShownWithBlobs ptjob = null;
		if (job != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String pttypeid;
			String cityid;
			String salaryunit;
			String billcycleid;
			String places = "";
			List<tbl_workplace> lplace = tbl_workplaceMapper.selectJobId(job.getId());
			for (tbl_workplace p : lplace) {
				String[] temp = p.getJobplace().split("&");
				String t = temp[0] + temp[1] + "\n";
				places += t;
			}
			cityid = job.getCityid() + "";
			tbl_employerinfo tbl_employerinfo = tbl_employerinfoMapper.selectByPrimaryKey(job.getMobile());
			cityid = tbl_divisionMapper.selectCountryName(cityid).getDiming();
			salaryunit = tbl_dictMapper.selectByPrimaryKey(job.getSalaryunit()).getValue();
			pttypeid = tbl_dictMapper.selectByPrimaryKey(job.getPttypeid()).getValue();
			billcycleid = tbl_dictMapper.selectByPrimaryKey(job.getBillcycleid()).getValue();
			String Genderne = tbl_dictMapper.selectByPrimaryKey(job.getGenderneedid()).getValue();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ptjobid", job.getId());
			map.put("status", 30);
			int applyed = tbl_jobenrollMapper.getReqJobUserCount(map);
			map.put("ptjobid", job.getId());
			map.put("status", 31);
			int confirmed = tbl_jobenrollMapper.getReqJobUserCount(map);
			ptjob = new PtJobForShownWithBlobs(job.getId() + "", job.getMobile(), job.getTitle(), pttypeid, cityid,
					sdf.format(job.getBegindate()), sdf.format(job.getEnddate()), job.getPersonnum() + "", Genderne,
					job.getSalary() + "", salaryunit, billcycleid, sdf.format(job.getCreatetime()),
					job.getInterview() + "", job.getTrain() + "", job.getTimebegin() + "", job.getTimeend(), 
					tbl_employerinfo.getLogo(), job.getRequirement(), job.getContect(),places,applyed,confirmed,
					tbl_employerinfo.getName(),tbl_employerinfo.getComment());

		}
		return ptjob;
	}
}